package cn.kone.controller.manager;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.kone.pojo.domain.Order;
import cn.kone.pojo.domain.OrderBill;
import cn.kone.pojo.dto.LocationRedisDTO;
import cn.kone.pojo.query.PageQueryWrapper;
import cn.kone.result.JSONResult;
import cn.kone.result.PageResult;
import cn.kone.service.IOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Tag(name = "订单表",description = "订单表")
@RestController
@RequestMapping("/manager/order")
public class OrderController{

    @Autowired
    public IOrderService orderService;

    //乘客下单后,保存到order表中
    @PostMapping("/addOrder")
    public JSONResult addOrder(@RequestBody Order order){
       return JSONResult.success(orderService.addOrder(order));
    }

    //乘客取消订单后,将order表中的状态修改
    @PostMapping("/updateOrder/{orderNo}")
    public JSONResult updateOrder(@PathVariable String orderNo){
        return JSONResult.success(orderService.updateOrder(orderNo));
    }

    //查询乘客订单状态,根据订单状态,前端跳转页面
    @GetMapping("/getOrder")
    public JSONResult getOrder(){
        return JSONResult.success(orderService.getOrder());
    }

    //拉取订单消息,在然后在司机端小程序页面展示订单详情
    @GetMapping("/pullOrder")
    public JSONResult pullOrder(){
        return JSONResult.success(orderService.pullOrder());
    }

    //司机抢单
    @GetMapping("/killOrder/{orderNo}")
    public JSONResult killOrder(@PathVariable String orderNo){
        return JSONResult.success(orderService.killOrder(orderNo));
    }

    //司机到达代驾地点更改订单状态为2已到达代驾点
    @PostMapping("/updateOrderStatus/{orderNo}")
    public JSONResult updateOrderStatus(@PathVariable String orderNo){
        return JSONResult.success(orderService.updateOrderStatus(orderNo));
    }

    //查询订单状态返回给前端,前端根据订单状态进行跳转页面
    @GetMapping("/loadOrder")
    public JSONResult loadOrder(){
        return JSONResult.success(orderService.loadOrder());
    }


    //司乘同显,通过订单号,返回给前端订单对象,返回给前端经纬度
    @GetMapping("/loadOrderLocation/{orderNo}")
    public JSONResult loadOrderLocation(@PathVariable String orderNo){
        return JSONResult.success(orderService.loadOrderLocation(orderNo));
    }


    //司机到达代驾地点,点击开始代驾,将订单状态修改为3开始代驾
    @PostMapping("/startDrivingHandle/{orderNo}")
    public JSONResult startDrivingHandle(@PathVariable String orderNo){
        return JSONResult.success(orderService.startDrivingHandle(orderNo));
    }

    //将司机的位置信息和下单后的订单编号缓存进Redis中
    @PostMapping("/LocationRedis")
    public JSONResult LocationRedis(@RequestBody LocationRedisDTO locationRedisDTO){
        return JSONResult.success(orderService.LocationRedis(locationRedisDTO));
    }

    //从Redis中获取到坐标和订单
    @GetMapping("/getLocationRedis/{orderNo}")
    public JSONResult getLocationRedis(@PathVariable String orderNo){
        return JSONResult.success(orderService.getLocationRedis(orderNo));
    }

    //查询订单和账单详情,返回给前端,给订单详情进行赋值回显
    @GetMapping("/getOrderDetail/{orderNo}")
    @SaIgnore
    public JSONResult getOrderDetail(@PathVariable String orderNo){
        return JSONResult.success(orderService.getOrderDetail(orderNo));
    }

    //app司机端点击结束代驾,修改订单状态
    @PostMapping("/endDrivingHandle/{orderNo}")
    public JSONResult endDrivingHandle(@PathVariable String orderNo){
        return JSONResult.success(orderService.endDrivingHandle(orderNo));
    }

    //司机确认费用
    @PostMapping("/confirmfree")
    public JSONResult confirmFree(@RequestBody OrderBill orderBill){
        return JSONResult.success(orderService.confirmFree(orderBill));
    }

    //前端发送账单确认费用,修改状态
    @PostMapping("/sendorderBill/{orderNo}")
    public JSONResult sendorderBill(@PathVariable String orderNo){
        return JSONResult.success(orderService.sendorderBill(orderNo));
    }





    @Operation( summary= "保存Order",description = "基础对象保存接口")
    @Parameter(name = "order",description = "保存的对象",required = true)
    @PostMapping
    public JSONResult save(@RequestBody @Valid Order order){
        return JSONResult.success(orderService.save(order));
    }

    @Operation( summary= "修改Order",description = "基础对象修改接口")
    @Parameter(name = "order",description = "修改的对象",required = true)
    @PutMapping
    public JSONResult update(@RequestBody  @Valid Order order){
        return JSONResult.success(orderService.updateById(order));
    }

    //删除对象
    @Operation( summary= "删除Order",description = "基础对象删除接口，采用状态删除")
    @Parameter(name = "id",description = "删除的对象ID",required = true)
    @DeleteMapping(value="/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        return JSONResult.success(orderService.removeById(id));
    }

    //获取对象
    @Operation( summary= "获取Order",description = "基础对象获取接口")
    @Parameter(name = "id",description = "查询的对象ID",required = true)
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(orderService.getById(id));
    }

    //获取列表:PageQueryWrapper作为通用的查询对象
    @Operation( summary= "查询Order列表",description = "基础对象列表查询，不带分页")
    @Parameter(name = "query",description = "查询条件",required = true)
    @PostMapping(value = "/list")
    public JSONResult list(@RequestBody PageQueryWrapper<Order> query){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        return JSONResult.success(orderService.list(wrapper));
    }

    //分页查询
    @Operation( summary= "查询Order分页列表",description = "基础对象列表查询，带分页")
    @Parameter(name = "query",description = "查询条件，需要指定分页条件",required = true)
    @PostMapping(value = "/pagelist")
    public JSONResult page(@RequestBody PageQueryWrapper<Order> query){
        //分页查询
        Page<Order> page = new Page<Order>(query.getPage(),query.getRows());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        //分页查询
        page = orderService.page(page,wrapper);
        //返回结果
        return JSONResult.success(new PageResult<Order>(page.getTotal(),page.getRecords()));
    }

}
